package org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.advanced;


import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.tcp.socket.NettyTcpClientPermeateClientVisitorSocket;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.tcp.client.AbstractHandleTcpDistributeClientPermeateClientInitTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.TcpMessageTypeEnums;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * 客户端渗透客户端init信息
 *
 * @see TcpMessageTypeEnums#TCP_DISTRIBUTE_CLIENT_PERMEATE_CLIENT_INIT
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
public class ClientHandleTcpDistributeClientPermeateClientInitTypeAdvanced
        extends AbstractHandleTcpDistributeClientPermeateClientInitTypeAdvanced<NettyProxyMsg> {

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext       当前通道
     * @param nettyProxyMsg 通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        // 初始化 客户端渗透服务端socket
        byte[] originClientIdBytes = nettyProxyMsg.getClientId();
        byte[] visitorPortBytes = nettyProxyMsg.getVisitorPort();
        byte[] clientTargetIpBytes = nettyProxyMsg.getTargetIp();
        byte[] clientTargetPortBytes = nettyProxyMsg.getTargetPort();
        byte[] targetClientIdBytes = nettyProxyMsg.getData();

        String originClientId = new String(originClientIdBytes);
        String targetClientId = new String(targetClientIdBytes);
        Integer visitorPort = Integer.parseInt(new String(visitorPortBytes));
        String clientTargetIp = new String(clientTargetIpBytes);
        Integer clientTargetPort = Integer.parseInt(new String(clientTargetPortBytes));

        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
//        ChannelFlowAdapter channelFlowAdapter = SpringContextHolder.getBean(ChannelFlowAdapter.class);
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
        log.info("client permeate client from client_id:【{}】 target_client_id【{}】with visitor_port【{}】, clientTargetIp:【{}】clientTargetPort:【{}】",
                originClientId, targetClientId, visitorPort, clientTargetIp, clientTargetPort);
        NettyTcpClientPermeateClientVisitorSocket nettyTcpClientPermeateClientVisitorSocket =
                NettyTcpClientPermeateClientVisitorSocket.NettyClientPermeateClientVisitorSocketBuilder.builder()
                        .builderClientId(originClientId)
                        .builderClientTargetIp(clientTargetIp)
                        .builderClientTargetPort(clientTargetPort)
                        .builderVisitorPort(visitorPort)
                        .builderNettyClientProperties(nettyClientProperties)
                        //.builderChannelFlowAdapter(channelFlowAdapter)
                        .builderToClientId(targetClientId)
                        .builderHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList)

                .build();
        try {
            nettyTcpClientPermeateClientVisitorSocket.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
